package com.ogawa.a7517.fragment;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ogawa.a7517.DataManager;
import com.ogawa.a7517.R;
import com.ogawa.a7517.adapter.AutoAdapter;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;
import com.ogawa.a7517.bean.ProgramBean;
import com.ogawa.a7517.utils.ViewUtil;
import com.ogawa.a7517.widget.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class CollectFragment extends BaseFragment {
    private TextView tvNoData;
    private AutoAdapter adapter;
    private List<ProgramBean> collectBeans;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_collect;
    }

    @Override
    public void initView(View view) {
        RecyclerView rlvCollect = view.findViewById(R.id.rlv_collect);
        ImageView ivComplete = view.findViewById(R.id.iv_complete);
        tvNoData = view.findViewById(R.id.tv_no_data);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
        rlvCollect.setLayoutManager(layoutManager);
        int topSpace = ViewUtil.dip2px(35, activity7517);
        SpaceItemDecoration itemDecoration = new SpaceItemDecoration(topSpace, 0, 1);
        rlvCollect.addItemDecoration(itemDecoration);
        collectBeans = new ArrayList<>();
        getCollectionBeans();
        adapter = new AutoAdapter(collectBeans, false);
        rlvCollect.setAdapter(adapter);
        adapter.setOnItemLongClickListener((adapter1, view1, position) -> {
            adapter.setShowDelete(true);
            ivComplete.setVisibility(View.VISIBLE);
            return true;
        });
        adapter.setOnItemClickListener((adapter, view13, position) -> {
            if (view13.isSelected()) {
                judgeToWhere(false);
            } else {
                ProgramBean programBean = collectBeans.get(position);
                activity7517.publish(programBean.getFunctionCode(), String.valueOf(programBean.getModel()));
            }
        });
        ivComplete.setOnClickListener(view12 -> {
            adapter.setShowDelete(false);
            ivComplete.setVisibility(View.GONE);
        });
        adapter.addChildClickViewIds(R.id.iv_delete);
        adapter.setOnItemChildClickListener((adapter1, view1, position) -> {
            List<Integer> collections = DataManager.getInst().getCollections();
            collections.remove(Integer.valueOf(collectBeans.get(position).getModel()));
            DataManager.getInst().setCollections(collections);
            adapter.getData().remove(position);
            adapter.notifyItemRemoved(position);
            if (adapter.getData().size() == 0) {
                tvNoData.setVisibility(View.VISIBLE);
                ivComplete.setVisibility(View.GONE);
            }
        });
        deviceStateChange();
    }

    private void getCollectionBeans() {
        List<Integer> collects = DataManager.getInst().getCollections();
        List<ProgramBean> programBeans = DataManager.getInst().getProgramBeans();
        if (collects.size() == 0) {
            return;
        }
        a:
        for (Integer collect : collects) {
            for (ProgramBean programBean : programBeans) {
                if (collect == programBean.getModel()) {
                    collectBeans.add(programBean);
                    continue a;
                }
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle(getString(R.string.collection));
        tvNoData.setVisibility(adapter.getData().size() == 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (stateBean != null) {
            if (stateBean.getFunctions().getAutoProgram() != null) {
                int auto = Integer.parseInt(stateBean.getFunctions().getAutoProgram().getModelValue());
                adapter.setRunning(auto);
            }
        } else {
            if (adapter != null) {
                adapter.setRunning(0);
            }
        }
    }
}
